#include <QDoubleValidator>
#include "neworderdialog.h"

NewOrderDialog::NewOrderDialog(char *symbol, double price, time_t time, BarWidget *barWidget, QWidget *parent)
: QDialog(parent)
, m_barWidget(barWidget)
, m_currPrice(price)
, m_time(time)
{
    setWindowTitle("New Order");
    strcpy(m_symbol, symbol);
    createWidgets();
    createConnections();
    setOrderType();
    barWidget->setNewOrderLine(true);
}

void NewOrderDialog::setOrderType()
{
    QString text = m_boxOrderType->currentText();
    if (text == QString("Buy")) {
        m_labelOrderPrice->setText("Buy Price");
    }
    else if (text == QString("Sell")) {
        m_labelOrderPrice->setText("Sell Price");
    }
    else if (text == QString("Buy Limit")) {
        m_labelOrderPrice->setText("Buy Limit Price");
    }
    else if (text == QString("Sell Limit")) {
        m_labelOrderPrice->setText("Sell Limit Price");
    }
    else if (text == QString("Buy Stop")) {
        m_labelOrderPrice->setText("Buy Stop Price");
    }
    else if (text == QString("Sell Stop")) {
        m_labelOrderPrice->setText("Sell Stop Price");
    }
    m_editOrderPrice->setText(QString::number(m_currPrice, 'g', 10));
    m_editTakeProfit->setText(QString::number(0));
    m_editStoploss->setText(QString::number(0));
}

void NewOrderDialog::newOrder()
{
    if (!checkVaild())
        return;
    OrderManager *orderManager = OrderManager::inst();
    QString text = m_boxOrderType->currentText();
    if (text == QString("Buy")) {
        orderManager->createBuyOrder(m_time, m_symbol, m_orderPrice, m_orderLots, m_orderStoploss, m_orderTakeProfit);
    }
    else if (text == QString("Sell")) {
        orderManager->createSellOrder(m_time, m_symbol, m_orderPrice, m_orderLots, m_orderStoploss, m_orderTakeProfit);
    }
    else if (text == QString("Buy Limit")) {
        orderManager->createBuyLimitOrder(m_time, m_symbol, m_orderPrice, m_orderLots, m_orderStoploss, m_orderTakeProfit);
    }
    else if (text == QString("Sell Limit")) {
        orderManager->createSellLimitOrder(m_time, m_symbol, m_orderPrice, m_orderLots, m_orderStoploss, m_orderTakeProfit);
    }
    else if (text == QString("Buy Stop")) {
    }
    else if (text == QString("Sell Stop")) {
    }
    close();
}

void NewOrderDialog::orderPriceChanged()
{
    double val = m_editOrderPrice->text().toDouble();
    m_barWidget->setNewOrderPrice(val);
    m_barWidget->update();
}

void NewOrderDialog::orderTakeProfitChanged()
{
    double val = m_editTakeProfit->text().toDouble();
    m_barWidget->setNewOrderTakeProfit(val);
    m_barWidget->update();
}

void NewOrderDialog::orderStoplossChanged()
{
    double val = m_editStoploss->text().toDouble();
    m_barWidget->setNewOrderStoploss(val);
    m_barWidget->update();
}

void NewOrderDialog::createWidgets()
{
    m_boxOrderType = new QComboBox();
    QStringList strListOrderType;
    strListOrderType << "Buy" << "Sell" << "Buy Limit" << "Sell Limit" << "Buy Stop" << "Sell Stop";
    m_boxOrderType->addItems(strListOrderType);

    QDoubleValidator *doubleValid = new QDoubleValidator(0, 10000, 5);

    m_labelLots = new QLabel("Lots");
    m_boxLots = new QComboBox();
    m_boxLots->setValidator(doubleValid);
    QStringList strListOrdeLots;
    strListOrdeLots << "0.1" << "0.2" << "0.3" << "0.4" << "0.5" << "0.6" << "0.7" << "0.8" << "0.9";
    strListOrdeLots << "1" << "2" << "3" << "4" << "5" << "6" << "7" << "8" << "9" << "10";
    m_boxLots->addItems(strListOrdeLots);
    QLayout *orderLotsLayout = new QHBoxLayout();
    orderLotsLayout->addWidget(m_labelLots);
    orderLotsLayout->addWidget(m_boxLots);

    m_labelOrderPrice = new QLabel("Price");
    m_editOrderPrice = new QLineEdit();
    m_editOrderPrice->setValidator(doubleValid);
    QLayout *orderPriceLayout = new QHBoxLayout();
    orderPriceLayout->addWidget(m_labelOrderPrice);
    orderPriceLayout->addWidget(m_editOrderPrice);

    m_labelTakeProfit = new QLabel("Take Profit");
    m_editTakeProfit = new QLineEdit();
    m_editTakeProfit->setValidator(doubleValid);
    QLayout *takeProfitLayout = new QHBoxLayout();
    takeProfitLayout->addWidget(m_labelTakeProfit);
    takeProfitLayout->addWidget(m_editTakeProfit);

    m_labelStoploss = new QLabel("Stop Loss  ");
    m_editStoploss = new QLineEdit();
    m_editStoploss->setValidator(doubleValid);
    QLayout *stopLossLayout = new QHBoxLayout();
    stopLossLayout->addWidget(m_labelStoploss);
    stopLossLayout->addWidget(m_editStoploss);

    m_btnOk = new QPushButton("OK");
    m_btnCancel = new QPushButton("Cancel");
    QLayout *okCancelLayout = new QHBoxLayout();
    okCancelLayout->addWidget(m_btnOk);
    okCancelLayout->addWidget(m_btnCancel);

    QBoxLayout *mainLayout = new QVBoxLayout();
    mainLayout->addWidget(m_boxOrderType);
    mainLayout->addLayout(orderLotsLayout);
    mainLayout->addLayout(orderPriceLayout);
    mainLayout->addLayout(takeProfitLayout);
    mainLayout->addLayout(stopLossLayout);
    mainLayout->addLayout(okCancelLayout);
    setLayout(mainLayout);
}

void NewOrderDialog::createConnections()
{
    connect(m_boxOrderType, SIGNAL(currentIndexChanged(int)), this, SLOT(setOrderType()));
    connect(m_btnOk, SIGNAL(clicked()), this, SLOT(newOrder()));
    connect(m_btnCancel, SIGNAL(clicked()), this, SLOT(close()));
    connect(m_editOrderPrice, SIGNAL(textChanged(const QString&)), this, SLOT(orderPriceChanged()));
    connect(m_editStoploss, SIGNAL(textChanged(const QString&)), this, SLOT(orderStoplossChanged()));
    connect(m_editTakeProfit, SIGNAL(textChanged(const QString&)), this, SLOT(orderTakeProfitChanged()));
}

bool NewOrderDialog::checkVaild()
{
    QString text = m_boxOrderType->currentText();
    m_orderLots = m_boxLots->currentText().toDouble();
    m_orderPrice = m_editOrderPrice->text().toDouble();
    m_orderStoploss = m_editStoploss->text().toDouble();
    m_orderTakeProfit = m_editTakeProfit->text().toDouble();
    if (m_orderLots < 0.1 || m_orderLots > 10) {
        return false;
    }
    if (m_orderPrice < 0.0 || m_orderPrice > 1000000) {
        return false;
    }
    if (text == QString("Buy") || text == QString("Sell")) {
        if (m_orderPrice != m_currPrice) {
            return false;
        }
    }
    else if (text == QString("Buy Limit") || text == QString("Sell Stop")) {
        if (m_orderPrice >= m_currPrice) {
            return false;
        }
    }
    else if (text == QString("Sell Limit") || text == QString("Buy Stop")) {
        if (m_orderPrice <= m_currPrice) {
            return false;
        }
    }
    if (m_orderStoploss != 0.0) {
        if (text == QString("Buy") || text == QString("Buy Limit") || text == QString("Buy Stop")) {
            if (m_orderStoploss >= m_orderPrice) {
                return false;
            }
        }
        if (text == QString("Sell") || text == QString("Sell Limit") || text == QString("Sell Stop")) {
            if (m_orderStoploss <= m_orderPrice) {
                return false;
            }
        }
    }
    if (m_orderTakeProfit != 0.0) {
        if (text == QString("Buy") || text == QString("Buy Limit") || text == QString("Buy Stop")) {
            if (m_orderTakeProfit <= m_orderPrice) {
                return false;
            }
        }
        if (text == QString("Sell") || text == QString("Sell Limit") || text == QString("Sell Stop")) {
            if (m_orderTakeProfit >= m_orderPrice) {
                return false;
            }
        }
    }
    return true;
}

void NewOrderDialog::closeEvent(QCloseEvent *e)
{
    m_barWidget->setNewOrderLine(false);
    QDialog::closeEvent(e);
}